<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="保单号" prop="contractNumber">
        <el-input
          v-model="queryParams.contractNumber"
          placeholder="请输入保单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保险公司名字" prop="applicantName">
        <el-input
          v-model="queryParams.applicantName"
          placeholder="请输入保险公司名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投保公司三合一码" prop="applicantCompany">
        <el-input
          v-model="queryParams.applicantCompany"
          placeholder="请输入投保公司三合一码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投保公司规模" prop="companySize">
        <el-select v-model="queryParams.companySize" placeholder="请选择投保公司规模" clearable>
          <el-option
            v-for="dict in dict.type.applicant_size"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="保险公司三合一码" prop="insuranceCompanyOrganization">
        <el-input
          v-model="queryParams.insuranceCompanyOrganization"
          placeholder="请输入保险公司三合一码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同状态" prop="contractCondition">
        <el-select v-model="queryParams.contractCondition" placeholder="请选择合同状态" clearable>
          <el-option
            v-for="dict in dict.type.contract_condition"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['insurance:contract:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['insurance:contract:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['insurance:contract:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['insurance:contract:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="保单号" align="center" prop="contractNumber" />
      <el-table-column label="保险公司名字" align="center" prop="applicantName" />
      <el-table-column label="投保公司三合一码" align="center" prop="applicantCompany" />
      <el-table-column label="投保公司规模" align="center" prop="companySize">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.applicant_size" :value="scope.row.companySize"/>
        </template>
      </el-table-column>
      <el-table-column label="保险公司三合一码" align="center" prop="insuranceCompanyOrganization" />
      <el-table-column label="签订日期" align="center" prop="signDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生效日期" align="center" prop="effectDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期日期" align="center" prop="deadlineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadlineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投保金额" align="center" prop="insuranceMoney" />
      <el-table-column label="合同状态" align="center" prop="contractCondition">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.contract_condition" :value="scope.row.contractCondition"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['insurance:contract:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['insurance:contract:remove']"
          >删除</el-button>
          <el-button
    size="mini"
    type="text"
    icon="el-icon-view"
    @click="handleViewDetails(scope.row)"
    v-hasPermi="['insurance:contract:view']"
  >索赔记录</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改保险合同对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="showfirst" label="保单号" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入保单号" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="保险公司名字" prop="applicantName">
          <el-input v-model="form.applicantName" placeholder="请输入保险公司名字" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="投保公司三合一码" prop="applicantCompany">
          <el-input v-model="form.applicantCompany" placeholder="请输入投保公司三合一码" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="投保公司规模" prop="companySize">
          <el-select v-model="form.companySize" placeholder="请选择投保公司规模">
            <el-option
              v-for="dict in dict.type.applicant_size"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showfirst" label="保险公司三合一码" prop="insuranceCompanyOrganization">
          <el-input v-model="form.insuranceCompanyOrganization" placeholder="请输入保险公司三合一码" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="签订日期" prop="signDate">
          <el-date-picker clearable
            v-model="form.signDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签订日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="showfirst" label="生效日期" prop="effectDate">
          <el-date-picker clearable
            v-model="form.effectDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="showfirst" label="到期日期" prop="deadlineTime">
          <el-date-picker clearable
            v-model="form.deadlineTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到期日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="showfirst" label="投保金额" prop="insuranceMoney">
          <el-input v-model="form.insuranceMoney" placeholder="请输入投保金额" />
        </el-form-item>
        <el-form-item v-if="showfirst" label="合同状态" prop="contractCondition">
          <el-select v-model="form.contractCondition" placeholder="请选择合同状态">
            <el-option
              v-for="dict in dict.type.contract_condition"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showfirst" label="具体的合同地址" prop="contractVirtualAddress">
          <file-upload v-model="form.contractVirtualAddress"/>
        </el-form-item>
        <el-divider v-if="showsecond" content-position="center">索赔记录信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-if="showsecond" type="primary" icon="el-icon-plus" size="mini" @click="handleAddClaimRecord">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-if="showsecond" type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteClaimRecord">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="claimRecordList" :row-class-name="rowClaimRecordIndex" @selection-change="handleClaimRecordSelectionChange" ref="claimRecord">
          <el-table-column v-if="showsecond" type="selection" width="50" align="center" />
          <el-table-column v-if="showsecond" label="序号" align="center" prop="index" width="50"/>
          <el-table-column v-if="showsecond" label="索赔日期" prop="claimDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.claimDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择索赔日期" />
            </template>
          </el-table-column>
          <el-table-column v-if="showsecond" label="索赔原因" prop="claimReason" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.claimReason" placeholder="请输入索赔原因" />
            </template>
          </el-table-column>
          <el-table-column v-if="showsecond" label="索赔陈述" prop="concreteClaimRecord" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.concreteClaimRecord" placeholder="请输入索赔陈述" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContract, getContract, delContract, addContract, updateContract } from "@/api/insurance/contract";

export default {
  name: "Contract",
  dicts: ['applicant_size', 'contract_condition'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedClaimRecord: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险合同表格数据
      contractList: [],
      // 索赔记录表格数据
      claimRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      showfirst: true, // 控制是否显示保险合同字段
      showsecond: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNumber: null,
        applicantName: null,
        applicantCompany: null,
        companySize: null,
        insuranceCompanyOrganization: null,
        contractCondition: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractNumber: [
          { required: true, message: "保单号不能为空", trigger: "blur" }
        ],
        applicantName: [
          { required: true, message: "保险公司名字不能为空", trigger: "blur" }
        ],
        applicantCompany: [
          { required: true, message: "投保公司三合一码不能为空", trigger: "blur" }
        ],
        insuranceCompanyOrganization: [
          { required: true, message: "保险公司三合一码不能为空", trigger: "blur" }
        ],
        contractCondition: [
          { required: true, message: "合同状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询保险合同列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractNumber: null,
        applicantName: null,
        applicantCompany: null,
        companySize: null,
        insuranceCompanyOrganization: null,
        signDate: null,
        effectDate: null,
        deadlineTime: null,
        insuranceMoney: null,
        contractCondition: null,
        contractVirtualAddress: null
      };
      this.claimRecordList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保险合同";
    },
      /** 查看详情按钮操作 */
      handleViewDetails(row) {
      this.reset();
      const id = row.id || this.ids;
      getContract(id).then(response => {
        this.form = response.data;
        this.claimRecordList = response.data.claimRecordList;
        this.open = true;
        this.showfirst=false;
        this.showsecond=true;
        this.title = "查看索赔记录";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data;
        this.claimRecordList = response.data.claimRecordList;
        this.open = true;
        this.showfirst=true;
        this.showsecond=false;
        this.title = "修改保险合同";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.claimRecordList = this.claimRecordList;
          if (this.form.id != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除保险合同编号为"' + ids + '"的数据项？').then(function() {
        return delContract(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 索赔记录序号 */
    rowClaimRecordIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 索赔记录添加按钮操作 */
    handleAddClaimRecord() {
      let obj = {};
      obj.claimDate = "";
      obj.claimReason = "";
      obj.concreteClaimRecord = "";
      this.claimRecordList.push(obj);
    },
    /** 索赔记录删除按钮操作 */
    handleDeleteClaimRecord() {
      if (this.checkedClaimRecord.length == 0) {
        this.$modal.msgError("请先选择要删除的索赔记录数据");
      } else {
        const claimRecordList = this.claimRecordList;
        const checkedClaimRecord = this.checkedClaimRecord;
        this.claimRecordList = claimRecordList.filter(function(item) {
          return checkedClaimRecord.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleClaimRecordSelectionChange(selection) {
      this.checkedClaimRecord = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('insurance/contract/export', {
        ...this.queryParams
      }, `contract_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
